package com.zsc.wxapp.services.impl;

import com.zsc.wxapp.entity.CollectedData;
import com.zsc.wxapp.entity.User;
import com.zsc.wxapp.entity.vo.CustomerVO;
import com.zsc.wxapp.exservices.Remote2;
import com.zsc.wxapp.mapper.CollectedDateMapper;
import com.zsc.wxapp.mapper.CollectedDataMapper;
import com.zsc.wxapp.mapper.UserMapper;
import com.zsc.wxapp.services.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
public class TaskServiceImpl implements TaskService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private CollectedDataMapper collectedDataMapper;
    @Resource
    private CollectedDateMapper collectedDateMapper;
    @Resource
    private Remote2 remote2;

    /**
     * 更新用户上传的次数数据，定时任务
     */
    @Override
    public void updateTodayData() {
        List<User> userList = userMapper.getAllUser();
        for (User user : userList) {
            log.info("用户：" + user + "更新数据");
            CollectedData collectedData;
            if (user.getPermission().equals("1") || user.getPermission().equals("2")) {
                // 该用户为终端用户，拥有custUuid
                collectedData = CollectedData.builder()
                        .userId(user.getUserId())
                        .submitTimes(user.getSubmitTimes())
                        .photoTimes(user.getPhotoTimes())
                        .maintenanceTimes(user.getMaintenanceTimes())
                        .printTimes(user.getPrintTimes())
                        .custType(user.getCustType())
                        .area(user.getArea())
                        .createTime(LocalDate.now())
                        .build();
            } else {
                // 该用户为业务层用户
                collectedData = CollectedData.builder()
                        .userId(user.getUserId())
                        .submitTimes(user.getSubmitTimes())
                        .photoTimes(user.getPhotoTimes())
                        .maintenanceTimes(user.getMaintenanceTimes())
                        .printTimes(user.getPrintTimes())
                        .custType(null)
                        .area(null)
                        .createTime(LocalDate.now())
                        .build();
            }
            log.info("要插入的数据为：{}", collectedData);
            collectedDataMapper.insert(collectedData);
            //归零user表中的次数数据
            userMapper.resetTimes(user.getUserId());
        }
    }

    /**
     * 更新当天上传数据
     */
    @Override
    public void updateTodayNum() {
        List<User> users = userMapper.getAllUser();
        LocalDate now = LocalDate.now();
        Integer times = 0;
        for (User user : users) {
            if (user.getUploadDate() == null) {
                continue;
            }
            if (user.getUploadDate().isEqual(now)) {
                times++;
            }
        }
        collectedDateMapper.insert(times, now);
    }
}
